package seqListPack;

import LinkListPack.GNode;
import MyBaseClass.IObject;

public class SeqList {
	private IObject[] listElem;
	private int curLen;
	//????? ??????
	public SeqList(){
		this.listElem=new IObject[50];
		this.curLen=0;
	}

	public SeqList(int maxzise){
		this.listElem=new IObject[maxzise];
		this.curLen=0;
	}

	//???????????
	public boolean add(IObject x){
		if(curLen==listElem.length){
			return false;
		}
        insert(curLen, x);
		curLen++;
		return true;
	}
	
	//???
	public boolean isEmpty() {
		return curLen==0;
	}

	//???
	public void clear() {
		curLen=0;
	}

	//?????????i???????x
	public boolean insert(int i,IObject x){
		if(curLen==listElem.length){
			System.out.println("????!??????");
			return false;
		}else if(i<0||i>curLen){
			System.out.println("???????????");
			return false;
		}
		for(int j=curLen;j>i;j--)
			listElem[j]=listElem[j-1];
		listElem[i]=x;
		curLen++;
		return true;
	}

	//???????????????
	public int indeOf(IObject x) {
		int j=0;
		while(j<curLen&&!listElem[j].equals(x))
			j++;
		if(j<curLen)
			return j;
		else
			return -1;
	}
	//????????????????
	public IObject indeOf(int index){
		if(index<1||index>curLen){
			System.out.println("没有此元素");
			return null;
		}
		return this.listElem[index-1];
	}
	
	public boolean remove(int i){
		if(i<0||i>curLen-1){
			System.out.println("没有此元素");
			return false;
		}
		for(int j=i;j<curLen-1;j++)
			listElem[j]=listElem[j+1];
		curLen--;
		return true;
	}
	
	public boolean updata(int index,IObject x){
		if(index-1<0||index-1>curLen){
			System.out.println("没有此元素");
			return false;
		}
		listElem[index-1]=x;
		return true;
	}

	public int length() {
		return curLen;
	}

	public void display() {
		for(int j=0;j<curLen;j++)
			System.out.println(listElem[j].toString());
		System.out.println();
	}

}
